{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "3e425db5",
   "metadata": {},
   "outputs": [],
   "source": [
    "def matrix_multiple(x, y):\n",
    "    if type(x[0])==int:\n",
    "        x=[x]\n",
    "    if type(y[0])==int:\n",
    "        y=[y]\n",
    "    result = [[sum(x[i][k] * y[k][j] for k in range(len(y))) for j in range(len(y[0]))] for i in range(len(x))]\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "f57d61c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert matrix_multiple([[3, 5], [2, 4]], [[1, 2, 5], [4, 3, 2]]) == [[23, 21, 25], [18, 16, 18]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "47a1fc69",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert matrix_multiple([2], [5]) == [[10]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "6a047b5e",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert matrix_multiple([[2]], [[5]]) == [[10]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "7b340ecf",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert matrix_multiple([[1, 3]], [[5], [2]]) == [[11]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "bc5516e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[0], [-5], [-9]]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matrix_multiple([[1,2,-3],[3,5,-2],[-1,4,0]], [[1],[-2],[-1]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "850254ba-a0ab-4ace-8da4-b3518592da98",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'k层：用sum累加k的内循环，代替“+=”迭代累加，更接近于最简矩阵乘法公式；\\nj,i层:sum的外循环,其中：\\nj层依据y的列数(即矩阵一行(一个第二层列表)中的元素个数)完成第二层列表(即矩阵的行内)的迭代构造;\\ni层依据x的行数(即其第一层列表的元素个数)完成第一层列表(即矩阵的行)的迭代构造'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "    #result = [[0 for i in range(len(y[0]))] for i in range(len(x))]\n",
    "    #初始化result二维列表，分别以x的行数，y的列数为行数，列数\n",
    "    #print (result)#测试初始化结果，实际应用请注释掉这行\n",
    "    ##初始化代码，列表推导式优化后已弃用\n",
    "    '''k层：用sum累加k的内循环，代替“+=”迭代累加，更接近于最简矩阵乘法公式；\n",
    "    j,i层:sum的外循环,其中：\n",
    "    j层依据y的列数(即矩阵一行(一个第二层列表)中的元素个数)完成第二层列表(即矩阵的行内)的迭代构造;\n",
    "    i层依据x的行数(即其第一层列表的元素个数)完成第一层列表(即矩阵的行)的迭代构造'''\n",
    "    #for i in range(len(x)):\n",
    "     #   for j in range(len(y[0])):\n",
    "      #      for k in range(len(y)):\n",
    "       #         result[i][j] += x[i][k] * y[k][j]\n",
    "        #原始三层for写法(需要初始化)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
